package com.nd.android.u.ui.chatUiUtils;

import ims.IMSdkEntry;
import ims.utils.CommUtil;

import java.util.regex.Matcher;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.DisplayMetrics;

import com.common.android.utils.smiley.Smiley;
import com.common.android.utils.smiley.Smileyhelper;
import com.nd.android.u.allCommonUtils.UrlUtils;
import com.nd.android.u.controller.ChatConst.MessageContentType;
import com.nd.android.u.controller.ResMapper;
import com.nd.android.u.controller.factory.ChatDaoFactory;
import com.nd.android.u.controller.factory.MessageFactory;
import com.nd.android.u.controller.innerInterface.IDbDataSupplier;
import com.nd.android.u.controller.innerInterface.IMessageDisplay;

/**
 * 聊天共通工具类
 * <br>Created 2014-9-22 下午5:43:45
 * @version  
 * @author   cb		
 * @see 	 
 */


public class ChatCommonUtils {
	/**
	 * 获取displaymetrics
	 * <br>Created 2014-9-22 下午5:44:02
	 * @return  DisplayMetrics
	 * @author  cb
	*/
	public static DisplayMetrics getMetrics(){
		return IMSdkEntry.INSTANCE.context.getResources().getDisplayMetrics();
	}
	
	/**
	 * 把文本消息转换成spannableString
	 * <br>Created 2014-7-22 下午5:04:44
	 * @param text 输入的文本
	 * @param forceAddSid 是否要强制在url的末尾添上sid
	 * @return 转换的结果
	 * @author       cb
	*/
	public static SpannableStringBuilder changeToSpan(String text,boolean forceAddSid){
		SpannableStringBuilder spannableStringBuilder=new SpannableStringBuilder();
		SpannableString spannableString=Smileyhelper.getInstance().getSmileySpannableString(text, Smiley.TYPE_CHAT);
		spannableStringBuilder.append(spannableString);
		
		Matcher matcher = UrlUtils.WEB_URL.matcher(text);
		int start=0;
		int end=0;
		while (matcher.find()){
			start = matcher.start();
			end = matcher.end();
			String urltext=text.substring(start, end);
			SpannableString urlSpannable=new SpannableString(urltext);
			urlSpannable = Smileyhelper.getInstance().getHtml(urlSpannable,urltext,forceAddSid);
			urlSpannable.setSpan(new ForegroundColorSpan(0xff20558a), 0, urltext.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			spannableStringBuilder.replace(start, end, urlSpannable);
		}
		return spannableStringBuilder;
	}
	
	/**
	 * 把字符串转成json
	 * <br>Created 2014-8-1 下午2:52:56
	 * @param str 字符串
	 * @return json
	 * @author  cb
	*/
	public static JSONObject analyMessageByJSON(String str){
		if(str==null||"".equals(str)){
			return null;
		}
		JSONObject json  = null;
		try {
			json = new JSONObject(str);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		catch(RuntimeException e){
			e.printStackTrace();
		}
		return json;
	}
	
	/**
	 * 获取最后一条消息的富文本
	 * <br>Created 2014-10-30 下午2:44:46
	 * @param text 消息内容
	 * @param type 转换的类型
	 * @param generateId 消息的ID
	 * @return 消息的富文本
	 * @author  cb
	*/
	public static SpannableString getSmileySpannableString(String text,int type,String generateId){
		String audio=IMSdkEntry.INSTANCE.context.getString(ResMapper.INSTANCE.MSG_AUDIO);
		SpannableString s=Smileyhelper.getInstance().getSmileySpannableString(text, type, Smiley.SIZE_TEXT);
		if (text.equals(audio)){
			int catagory=CommUtil.getCategoryFromGenerateId(generateId);
			IMessageDisplay msg=MessageFactory.INSTANCE.getMessage(catagory, "0",0);
			IDbDataSupplier support=msg.getDbOperationSupport();
			if (ChatDaoFactory.getInstance().get(catagory).getData(support)){
				int contentType=msg.getMessageContentType();
				if (contentType==MessageContentType.AUDIO ||
					contentType==MessageContentType.OLD_AUDIO ){
					if (!msg.isRead()){
						s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
					}
				}
			}
		}
		return s;
	}
}
